package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.ExchangeList;
import com.baomidou.ant.entity.PointsStore;
import com.baomidou.ant.entity.User;
import com.baomidou.ant.entity.UserBag;
import com.baomidou.ant.mapper.ExchangeListMapper;
import com.baomidou.ant.service.IExchangeListService;
import com.baomidou.ant.service.IPointsStoreService;
import com.baomidou.ant.service.IUserBagService;
import com.baomidou.ant.service.IUserService;
import com.baomidou.ant.vo.Request.RequestExchangeListOfAdminVo;
import com.baomidou.ant.vo.Request.RequestExchangeListVo;
import com.baomidou.ant.vo.Request.RequestUseGoodsVo;
import com.baomidou.ant.vo.Response.ResponseExchangeListOfAdminVo;
import com.baomidou.ant.vo.Response.ResponseExchangeListVo;
import com.baomidou.ant.vo.Response.ResponseMyBagListVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service
public class ExchangeListServiceImpl extends ServiceImpl<ExchangeListMapper, ExchangeList> implements IExchangeListService {
    @Autowired
    private IPointsStoreService pointsStoreService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserBagService userBagService;


    @Override
    public IPage<ResponseExchangeListVo> list(RequestExchangeListVo requestExchangeListVo) {
        IPage<ExchangeList> iPage=new Page<>(requestExchangeListVo.getDisplayStart(),requestExchangeListVo.getDisplayLength());
        QueryWrapper<ExchangeList> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(requestExchangeListVo.getGoodsName())){
            queryWrapper.like("goods_name",requestExchangeListVo.getGoodsName());
        }
        IPage<ExchangeList> exchangeListIPage=page(iPage,queryWrapper);
        if(exchangeListIPage.getRecords().size()<=0) {
            return null;
        }
        return exchangeListIPage.convert(exchangeList -> {
            ResponseExchangeListVo responseExchangeListVo=new ResponseExchangeListVo();
            BeanUtils.copyProperties(exchangeList,responseExchangeListVo);
            //连表查询
            PointsStore goods=pointsStoreService.getById(responseExchangeListVo.getGoodsId());
            responseExchangeListVo.setGoodsImage(goods.getGoodsImage());
            responseExchangeListVo.setPrice(goods.getPrice());
            //返回
            return responseExchangeListVo;
        });
    }


    @Override
    public IPage<ResponseExchangeListOfAdminVo> listOfAdmin(RequestExchangeListOfAdminVo requestExchangeListOfAdminVo) {
        IPage<ExchangeList> iPage=new Page<>(requestExchangeListOfAdminVo.getDisplayStart(),requestExchangeListOfAdminVo.getDisplayLength());
        QueryWrapper<ExchangeList> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(requestExchangeListOfAdminVo.getGoodsName())){
            queryWrapper.like("goods_name",requestExchangeListOfAdminVo.getGoodsName());
        }
        if(StringUtils.isNotEmpty(requestExchangeListOfAdminVo.getUserName())){
            queryWrapper.like("user_name",requestExchangeListOfAdminVo.getGoodsName());
        }
        if(ObjectUtils.isNotEmpty(requestExchangeListOfAdminVo.getTime())){
            queryWrapper.like("create_time",requestExchangeListOfAdminVo.getTime());

        }
        IPage<ExchangeList> exchangeListIPage=page(iPage,queryWrapper);
        if(exchangeListIPage.getRecords().size()<=0) {
            return null;
        }
        return exchangeListIPage.convert(exchangeList -> {
            ResponseExchangeListOfAdminVo vo=new ResponseExchangeListOfAdminVo();
            BeanUtils.copyProperties(exchangeList,vo);
            vo.setTelephone(userService.getById(exchangeList.getUserId()).getTelephone());
            return vo;
        });
    }
}
